import cgi
import os
import wsgiref.handlers
import json
import classloader
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
import types

import inspect


debug = False;
class MainPage(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

class MainProcess(webapp.RequestHandler):
    def post(self):
        #self.response.out.write(json.write(self.request.get("appEngineClass")))
        if debug : print(self.request.get("appEngineClass"))
        objAction = classloader.getModule(self.request.get("appEngineClass"))
        strMethod = self.request.get("appEngineMethod")
        if debug : print(strMethod)
        str =eval("'"+ self.request.get("appEngineParam")+"'")
        if debug : print(str)
        lisParams = json.read(str)
        result = classloader.applyFuc(objAction,strMethod,lisParams)
        self.response.out.write(json.write(result))
        
        
        
def main():
  application = webapp.WSGIApplication(
                                       [('/process',MainProcess),('/', MainPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()